define(["jquery","bbcare","as/app/growth/growthAdd.servers","as/app/growth/growthAdd.ui"],function($,$bb,$servers,$ui){
	var event = {};
	var userId;

	/**
	 * 选择患者
	 */
	var onPatientSelect = function(){
		$(this).parent("ul").removeClass("tipInfo tipList").addClass("hide");
		var rows = $(this).data("rows");
		var index = $(this).index();
		var info = rows[index];
        $ui.info({
        	"id":"patientInfo",
        	"info":info
        });
        $("#patientInfo").attr("data-userId",info.userId);
	}

	/**
	 * 根据手机号、姓名、科研编号查询患者信息成功后的回调函数
	 */
	var ajaxPatientsByPhoneNoCallBack = function(data){
        if( data.rows.length > 0 ){
            $(".infoList > ul").removeClass("tipInfo hide").addClass("tipList");
            $ui.patients({
            	"id":"infoLists",
            	"list":data.rows
            });
            $(".infoList li").unbind("click");
            $(".infoList li").data({"rows":data.rows});
            $(".infoList li").click(onPatientSelect);
        }else{
            $(".infoList > ul").html("系统未找到该患者，请确保该患者已经建档").removeClass("tipList hide").addClass("tipInfo");
        }
	}

	/**
	 * 搜索
	 */
	event.search = function(){
		var val = $.trim( $("#search").val() );
        if( val != "" || val.length != 0 ){
        	var params = {"queueUserType":"0"};
        	$servers.isPhoneNo(val) ? params.phoneNo = val : params.userName = val;
            $servers.ajaxPatientsByPhoneNo(params,ajaxPatientsByPhoneNoCallBack);
        }else{
        	$(this).attr("id") == "search" ? $(".infoList > ul").html("") : $(".infoList > ul").html("请输入查询内容").removeClass("tipList hide").addClass("tipInfo");
        }
	}

	/**
	 * 身高、体重、头围输入
	 */
    event.input = function(){
    	var val = $.trim( $(this).val() );
        if( val && val != "" && !$servers.exp(val) ){
            $(this).next().html("请输入大于0的整数").addClass("tipInfo");
        }else{
        	$(this).next().html("").removeClass("tipInfo");
        }
    }

    /**
     * 提交成功后的回调函数
     */
    var ajaxSubmitCallBack = function(data,params){
        var obj = {
            "recordId":data.recordId,
            "userId":params.queueUserId
        }
        $bb.openPage("growthDetail",obj);
    }

    /**
     * 提交
     */
    event.onSubmit = function(){
    	var userId = $("#patientInfo").attr("data-userId");
    	if( userId ){
    		var weight = $.trim( $("#weight").val() ) / 1000;
            var height = $.trim( $("#height").val() ) / 10;
            var head = $.trim( $("#head").val() ) / 10;
            if( weight && height && head ){
            	var channelId = $bb.getChannelId();
            	var doctorName = JSON.parse($bb.getSessionStorage("account")).alias;
            	var userName = $("#patientInfo  .userName").text();
            	var params = {
			        "oprInfo": {
			            "opCode": "1011",
			            "opType": "2",
			            "opNote":"【"+ doctorName +"】医生为【"+ userName +"】患者上传生长发育评估数据",
			            "channelType": "2",
			            "channelId": channelId
			        },
			        "queueUserId": userId,
			        "weight": weight.toString(),
			        "height": height.toString(),
			        "headCircumference": head.toString(),
			    };
                $servers.ajaxSubmit(params,ajaxSubmitCallBack);
            }else{
                $bb.toastShow("身高、体重、头围不能为空，请按要求输入正确的格式",1500);
            }
        }else{
            $bb.toastShow("请先查询选择需要上传的用户！",1500);
        }
    }

	return event;
});